package org.bigjava.novelplatform.controller;

import org.bigjava.novelplatform.common.enums.UserRole;
import org.bigjava.novelplatform.common.exception.ExceptionCodeMessage;
import org.bigjava.novelplatform.common.exception.UserActionException;
import org.bigjava.novelplatform.common.utils.R;
import org.bigjava.novelplatform.entity.Recommend;
import org.bigjava.novelplatform.entity.Users;
import org.bigjava.novelplatform.service.RecommendService;
import org.bigjava.novelplatform.service.impl.AdminServiceImpl;
import org.bigjava.novelplatform.vo.BookVoList;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Resource
    RecommendService recommendService;
    @Resource
    AdminServiceImpl adminService;

    @PostMapping("/img")//添加一个推荐
    public R sendRegisterCode(
            @RequestPart(value = "file", required = false) MultipartFile multipartFile,
            @RequestPart(value = "name", required = false) String name,
            @RequestPart(value = "type", required = false) String type,
            @RequestPart(value = "region", required = false) String region,
            @RequestPart(value = "body", required = false) String body,
            @RequestPart(value = "copywriting", required = false) String copywriting
    ) {
        Recommend recommend = new Recommend(null, name, type, region, body, null, copywriting);
        Boolean b = recommendService.addRecommend(recommend, multipartFile);//添加一个推荐
        return R.ok();
    }

    @GetMapping("/getBookVoList")//书籍搜索
    public List<BookVoList> geBookVoList(@RequestParam String input) {
        List<BookVoList> list = recommendService.getSuggestVoByInput(input);
        System.out.println(list);
        return list;
    }

    @GetMapping("/getAllBodyListByKey")//获取redis所有推荐的key集合
    public R getAllBodyListByKey() {
        List<String> list = recommendService.getRecommendList();//获取所有key集合
        if (list == null) {
            return null;
        }
        ArrayList<HashMap<String, String>> list1 = new ArrayList<>();
        for (String s : list) {
            //HashMap<String, String> map = recommendService.getBodyByKey(s);//通过key获取内容
            list1.add(recommendService.getBodyByKey(s));
        }
        return R.ok().setData(list1);
    }

    @GetMapping("/getAllType")//获取redis所有类型
    public R getAllType() {
        List<String> list = recommendService.getAllType();//获取所有key集合
        return R.ok().setData(list);
    }

    @GetMapping("/getTypeTheRecommend")//获取某种类型的所有集合
    public R getAllRecommendByType(String type) {
        List<Recommend> list = recommendService.getAllRecommendByType(type);
        return R.ok().setData(list);
    }

    /**
     * 删除key
     * @param key
     * @return
     */
    @GetMapping("/del")
    public R removeKey(String key) {
        recommendService.removeKey(key);
        return R.ok();
    }

    @PostMapping("/addUser")
    public String addUser(@RequestBody Map<String, String> map) {
        Users users = new Users();
        users.setUserType(UserRole.USER);
        if ("普通用户".equals(map.get("userType"))) {
            users.setUserType(UserRole.USER);
        }
        if ("作者用户".equals(map.get("userType"))) {
            users.setUserType(UserRole.WRITER);
        }
        if ("编辑用户".equals(map.get("userType"))) {
            users.setUserType(UserRole.EDITOR);
        }
        users.setNickName(map.get("username"));
        users.setPassword(map.get("password"));
        users.setEmail(map.get("email"));
        if (adminService.addUser(users)) {
            return "添加成功";
        }
        return "添加失败";
    }

    @PostMapping("/searchUser")//搜索用户
    public List<Users> searchUser(@RequestBody Map<String, String> map) {
        return adminService.searchUser(map.get("boxBody"), map.get("rule"));
    }

    @PostMapping("/deleteUser")//删除用户
    public Boolean deleteUser(@RequestBody Map<String, String> map) {
        return (adminService.deleteUser(map.get("email")));
    }

    @PostMapping("/recoveryUser")//恢复已删除的用户
    public Boolean recoveryUser(@RequestBody Map<String, String> map) {
        return (adminService.recoveryUser(map.get("email")));
    }

    //{"message":'',"code":0,"data":""}
    @PostMapping("/getDownholeCount")//获取本章说的点踩数集合
    public List<Integer> getDownholeCount(@RequestBody Map<String, String> map) {
        String sizef = map.get("intervalSize");
        if (null == sizef) {
            throw new UserActionException(ExceptionCodeMessage.NON_COMPLIANT_PARAMETERS);
        }
        Integer size = 10;
        try {
            size = Integer.valueOf(sizef);
        } catch (Exception e) {
            throw new UserActionException(ExceptionCodeMessage.NON_COMPLIANT_PARAMETERS);
        }
        return adminService.getDownholeCount(size);
    }

    @PostMapping("/deleteDownholeCount")//删除超过指定点踩数的所有本章说
    public Boolean deleteDownholeCount(@RequestBody Map<String, String> map) {
        String sizef = map.get("deleteSize");
        if (null == sizef) {
            return false;
        }
        Integer size = null;
        try {
            size = Integer.valueOf(sizef);
        } catch (Exception e) {
            throw new UserActionException(ExceptionCodeMessage.NON_COMPLIANT_PARAMETERS);
        }
        return adminService.deleteDownholeCount(size);
    }

    @PostMapping("/getUserForType")
    public R getUserForType(@RequestBody Map<String, Map<String, String>> map) {
        String type1 = map.get("type").get("currentlySelectedUserType");
        String type2 = map.get("type").get("currentlySelectedUserTypeClassification");
        System.out.println(map);
        //{"message":'',"code":0,"data":""}
        R r = new R();
        r.put("message","获取数据成功");
        r.setData(adminService.getUserForType(type1,type2));
        return r;
    }

    @PostMapping("/getInitializationData")
    public R getInitializationData() {
        //{"message":'',"code":0,"data":""}
        R r = new R();
        r.put("message","获取数据成功");
        r.setData(adminService.getInitializationData());
        return r;
    }
}
